import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PipelineServiceProxy, PipelineListDto } from '@shared/service-proxies/service-proxies';

import { finalize } from 'rxjs/operators';
import * as moment from 'moment';

@Component({
  selector: 'warningTimeModal',
  templateUrl: './waring-time-modal.component.html'
})

export class WarningTimeModalComponent extends AppComponentBase {

  @ViewChild('actionModal') modal: ModalDirective;
  @ViewChild('warningDatePicker') warningDatePicker: ElementRef;

  saving = false;
  active = false;
  warningDate=new Date();
  //warningDate: moment.Moment = undefined;
  pipeline = new PipelineListDto();
  component: any = {};

  constructor(
    injector: Injector,
    private _pipelineService: PipelineServiceProxy
  ) {
    super(injector);
  }

  show(pipeline: PipelineListDto , component): void {
    this.active = true;

    this.pipeline = pipeline;
    this.component = component;

    setTimeout(() => {
      // $(this.warningDatePicker.nativeElement).datetimepicker({
      //   locale: 'zh-CN',
      //   format: 'YYYY年M月D日'
      // }).on('dp.change', e => {
      //   this.warningDate = moment.utc(e.date);
      // });

      this.modal.show();
    }, 0);


  }

  save(): void {
    this.saving = true;

    this._pipelineService.setActWarningDate(this.pipeline.id, this.component.seq,moment(this.warningDate))
      .pipe(finalize(() => {
        this.close();
        this.saving = false;
      }))
      .subscribe((result) => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.component.warningDateStr =moment(this.warningDate).format('YYYY-MM-DD');
      });
  }

  close(): void {
    this.modal.hide();
  }
}
